﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNSClient
{
    public partial class uc_Thembac : UserControl
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        public uc_Thembac()
        {
            InitializeComponent();
        }

        private void uc_Thembac_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        public void LoadData()
        {
            gv_dsThembac.DataSource = sv.getBL();
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            txt_MaBL.Enabled = false;
            gridView1.Columns[0].Caption = "Mã bậc lương";
        }

        private void btn_ThemNgach_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            txt_MaBL.Enabled = false;

            List<QLNSServiceReference.BAC_LUONG> listLastBL = new List<QLNSServiceReference.BAC_LUONG>(sv.getLastBL());
            int a = listLastBL[0].Ma_BL;
            txt_MaBL.Text = (a + 1).ToString();
            
            
        }

        private void btn_LuuNgach_Click(object sender, EventArgs e)
        {
            string a = txt_MaBL.Text;
            btn_Luu.Enabled = true;

            QLNSServiceReference.BAC_LUONG bl = new QLNSServiceReference.BAC_LUONG();
           
            
                bl.Ma_BL = Int32.Parse(a);
                
                int kq = sv.addBL(bl);
                if (kq != -1)
                {
                    LoadData();
                    MessageBox.Show("Thêm bậc lương '"+txt_MaBL.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Bậc lương đã tồn tại","Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           
        }


        string mabl = "";
        private void gv_dsThembac_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            txt_MaBL.Enabled = false;

            try
            {
                txt_MaBL.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]).ToString();
            }
            catch { }

           // mabl = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0].ToString());
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;

            
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteBL(mabl);
                LoadData();
                MessageBox.Show("Xóa thành công!");
            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void labelControl4_Click(object sender, EventArgs e)
        {

        }
    }
}
